<?php

namespace App\Http\Controllers\V1\Api;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class FileController extends BaseController
{

    public function upload(Request $request)
    {
        $file = $request->file('file');
        $folder = $request->input('folder');
        if (empty($folder)) {
            $folder = "uploads";
        }
        // 2.是否符合文件类型 getClientOriginalExtension 获得文件后缀名
        $fileExtension = $file->getClientOriginalExtension();

        //原上传文件名称
        $clientName = $file->getClientOriginalName();

        // 5.每天一个文件夹,分开存储, 生成一个随机文件名
        $fileName = $folder . "/" . date('Y_m_d') . '/' . md5($clientName) . '.' . $fileExtension;
        $disk = Storage::disk('oss');
        $disk->put($fileName, file_get_contents($file));
        $url = $disk->getUrl($fileName);

        return ajax_output(200, "成功", $url);
    }


    public function uploadAll(Request $request)
    {
        $file = $request->file('file');
        $folder = $request->input('folder');
        if (empty($folder)) {
            $folder = "uploads";
        }
        // 2.是否符合文件类型 getClientOriginalExtension 获得文件后缀名
        $fileExtension = $file->getClientOriginalExtension();

        //原上传文件名称
        $clientName = $file->getClientOriginalName();

        // 5.每天一个文件夹,分开存储, 生成一个随机文件名
        $fileName = $folder . "/" . date('Y_m_d') . '/' . md5($clientName) . '.' . $fileExtension;
        $disk = Storage::disk('oss');
        $disk->put($fileName, file_get_contents($file));
        $url = $disk->getUrl($fileName);
        $size = $disk->size($fileName);
        $file_name = explode('.', $clientName);


        $data['url'] = $url;
        $data['size'] = $size;
        $data['file_name'] = $file_name[0];

        return ajax_output(200, "成功", $data);
    }

}
